package cn.javase.concurrent._并发编程._06_共享模型之无锁._原子引用._示例;

import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicReference;

public class DecimalAccountSafeCAS implements DecimalAccount {

    private AtomicReference<BigDecimal> ref;

    public DecimalAccountSafeCAS(String balance) {
        this.ref = new AtomicReference<>(new BigDecimal(balance));
    }

    @Override
    public BigDecimal getBalance() {
        return this.ref.get();
    }

    @Override
    public void withdraw(BigDecimal amount) {
        this.ref.updateAndGet(ref -> ref.subtract(amount));
    }

}
